package com.xintai.approval.service.impl;

import java.util.Date;
import java.util.List;

import com.xintai.approval.Utils.CommitUtils;
import com.xintai.approval.domain.FlowApply;
import com.xintai.approval.domain.FlowCaigouApply;
import com.xintai.approval.mapper.FlowApplyMapper;
import com.xintai.approval.mapper.FlowCaigouApplyMapper;
import com.xintai.common.core.domain.entity.SysUser;
import com.xintai.common.utils.SecurityUtils;
import com.xintai.common.utils.StringUtils;
import com.xintai.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;
import com.xintai.approval.mapper.FlowWorkflowMapper;
import com.xintai.approval.domain.FlowWorkflow;
import com.xintai.approval.service.IFlowWorkflowService;

import javax.annotation.Resource;
import javax.validation.constraints.Size;

/**
 * 工作流程Service业务层处理
 *
 * @author xintai
 * @date 2021-02-01
 */
@Service
public class FlowWorkflowServiceImpl implements IFlowWorkflowService
{
    private static String agree = "1"; //同意
    private static String refuse = "-1"; //拒绝
    private static String no = "0"; //未审核
    private static String reading = "3"; //审核中

    @Resource
    private FlowWorkflowMapper flowWorkflowMapper;
    @Resource
    private FlowCaigouApplyMapper flowCaigouApplyMapper;
    @Resource
    private FlowApplyMapper flowApplyMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    /**
     * 查询工作流程
     *
     * @param id 工作流程ID
     * @return 工作流程
     */
    @Override
    public FlowWorkflow selectFlowWorkflowById(Long id)
    {
        return flowWorkflowMapper.selectFlowWorkflowById(id);
    }

    /**
     * 查询工作流程列表
     *
     * @param flowWorkflow 工作流程
     * @return 工作流程
     */
    @Override
    public List<FlowWorkflow> selectFlowWorkflowList(FlowWorkflow flowWorkflow)
    {
        flowWorkflow.setStatus("1");
        return flowWorkflowMapper.selectFlowWorkflowList(flowWorkflow);
    }

    /**
     * 新增工作流程
     *
     * @param flowWorkflow 工作流程
     * @return 结果
     */
    @Override
    public int insertFlowWorkflow(FlowWorkflow flowWorkflow)
    {
        int i = 0;
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        Long userId = Long.valueOf(flowWorkflow.getApproverUid());
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        String changeName = sysUser.getNickName();

        //保存节点信息
        FlowWorkflow flowWorkflow1 =new FlowWorkflow();
        flowWorkflow1.setApproverUid(flowWorkflow.getApproverUid());
        flowWorkflow1.setUpNodeId(flowWorkflow.getUpNodeId());
        flowWorkflow1.setDownNodeId(flowWorkflow.getDownNodeId());
        flowWorkflow1.setNodeType("1");
        flowWorkflow1.setStatus("1");
        flowWorkflow1.setOrderNum(flowWorkflow.getOrderNum()+1);
        flowWorkflow1.setApplyId(flowWorkflow.getApplyId());
        i = flowWorkflowMapper.insertFlowWorkflow(flowWorkflow1);

        //修改申请
        FlowApply flowApply = flowApplyMapper.selectFlowApplyById(flowWorkflow.getApplyId());
        flowApply.setNodeId(flowWorkflow1.getId());
        flowApplyMapper.updateFlowApply(flowApply);

        //修改上级节点
        FlowWorkflow flowWorkflow2 = flowWorkflowMapper.selectFlowWorkflowById(flowWorkflow.getId());
        flowWorkflow2.setApproverRemark(nickName + "转交给" + changeName);
        flowWorkflow2.setStatus("0");
        flowWorkflow2.setChangeTime(new Date());
        flowWorkflow2.setChangeRemark(flowWorkflow.getChangeRemark());
        i = flowWorkflowMapper.updateFlowWorkflow(flowWorkflow2);
        return i;
    }

    /**
     * 修改工作流程
     *
     * @param flowWorkflow 工作流程
     * @return 结果
     */
    @Override
    public int updateFlowWorkflow(FlowWorkflow flowWorkflow)
    {
        return flowWorkflowMapper.updateFlowWorkflow(flowWorkflow);
    }

    /**
     * 批量删除工作流程
     *
     * @param ids 需要删除的工作流程ID
     * @return 结果
     */
    @Override
    public int deleteFlowWorkflowByIds(Long[] ids)
    {
        return flowWorkflowMapper.deleteFlowWorkflowByIds(ids);
    }

    /**
     * 删除工作流程信息
     *
     * @param id 工作流程ID
     * @return 结果
     */
    @Override
    public int deleteFlowWorkflowById(Long id)
    {
        return flowWorkflowMapper.deleteFlowWorkflowById(id);
    }

    /**
     * 修改 申请
     * @param flowApply
     * @return
     */
    @Override
    public int updateWorkFlowApply(FlowApply flowApply) {
        //获取流程表
        FlowWorkflow flowWorkflow = flowWorkflowMapper.selectFlowWorkflowById(flowApply.getNodeId());
        FlowWorkflow flowWorkflowDown=null;
        FlowCaigouApply flowCaiGouApply = flowApply.getFlowCaiGouApply();
        String s = CommitUtils.arrToStr(flowCaiGouApply.getArrCaigou());
        flowCaiGouApply.setCaigouType(s);
        //0：未审核，1:正在审核, -1:拒绝 , 4:表示完成
        if (flowApply.getStatus().equals(agree)){
            /*同意*/
            //修改当前节点
            flowWorkflow.setStatus(no);
            flowWorkflow.setApprovalTime(new Date());
            flowWorkflow.setApproverRemark(flowApply.getApproverRemark());
            //当前节点不为最后节点
            if (!flowWorkflow.getDownNodeId().equals(0L)){
                //获取下级节点
                flowWorkflowDown = flowWorkflowMapper.selectFlowWorkflowById(flowWorkflow.getDownNodeId());
            }else {
                //修改申请单
                flowApply.setStatus("4");
                flowApplyMapper.updateFlowApply(flowApply);
                flowWorkflowMapper.updateFlowWorkflow(flowWorkflow);
            }
            try {
                //是否为抄送人
                if (flowWorkflowDown.getNodeType().equals("2")){
                    if (flowWorkflowDown.getDownNodeId().equals(0L)){
                        flowApply.setStatus("4");
                        flowApply.setNodeId(flowWorkflowDown.getId());
                        flowWorkflowDown.setStatus(no);
                        //修改申请单
                        flowWorkflow.setStatus(no);
                        flowWorkflowDown.setApprovalTime(new Date());
                        flowApplyMapper.updateFlowApply(flowApply);
                        flowWorkflowMapper.updateFlowWorkflow(flowWorkflow);
                        flowWorkflowMapper.updateFlowWorkflow(flowWorkflowDown);
                    }else {
                        //修改抄送时间
                        flowWorkflowDown.setApprovalTime(new Date());
                        //获取下一个审核人
                        FlowWorkflow flowWorkflow1 = flowWorkflowMapper.selectFlowWorkflowById(flowWorkflowDown.getDownNodeId());
                        //修改下级节点
                        flowWorkflow1.setStatus(agree);
                        //修改申请单
                        flowApply.setNodeId(flowWorkflow1.getId());
                        flowApply.setStatus(reading);
                        //修改申请单
                        flowApplyMapper.updateFlowApply(flowApply);
                        //修改流程表
                        //抄送节点
                        flowWorkflowMapper.updateFlowWorkflow(flowWorkflowDown);
                        //当前节点
                        flowWorkflowMapper.updateFlowWorkflow(flowWorkflow);
                        //下一级节点
                        flowWorkflowMapper.updateFlowWorkflow(flowWorkflow1);
                    }
                }else {
                    //判断当前是否最后一节点
                    if (flowWorkflow.getDownNodeId().equals(0L)){
                        flowApply.setStatus("4");
                        //修改申请单
                        flowApplyMapper.updateFlowApply(flowApply);

                    }else {
                        flowWorkflowDown.setStatus(agree);
                        flowApply.setNodeId(flowWorkflowDown.getId());
                        flowApply.setStatus(reading);
                        //修改申请单
                        flowApplyMapper.updateFlowApply(flowApply);
                        //修改流程表
                        flowWorkflowMapper.updateFlowWorkflow(flowWorkflow);
                        flowWorkflowMapper.updateFlowWorkflow(flowWorkflowDown);
                    }
                }
            }catch (Exception e){}
        }else if (flowApply.getStatus().equals(refuse)){
            //拒绝
            flowWorkflow.setApproverRemark(flowApply.getApproverRemark());
            flowWorkflow.setStatus(refuse);
            flowApply.setStatus(refuse);
            flowApplyMapper.updateFlowApply(flowApply);
            flowWorkflowMapper.updateFlowWorkflow(flowWorkflow);
        }
        //修改采购信息
        return flowCaigouApplyMapper.updateFlowCaigouApply(flowCaiGouApply);
    }
}
